package com.xuecheng.media.controller;

import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.api.media.MediaApi;
import com.xuecheng.api.media.model.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class MediaController implements MediaApi {

    @Autowired
    private MediaService mediaService;


    /**
     * 保存数据
     * @param vo
     * @return
     */
    @PostMapping("media")
    public MediaDTO createMedia(@RequestBody MediaVO vo) {
        MediaDTO mediaDTO = MediaConvert.INSTANCE.vo2dto(vo);
        Long companyId = SecurityUtil.getCompanyId();
        mediaDTO.setCompanyId(companyId);
        return  mediaService.createMedia(mediaDTO);
    }


    /*
     * SpringMVC 的传入参数默认使用的是：QueryString
     *       如果参数的key值和封装类型的属性名一致，@QueryParam注解可以省略掉
     *
     * */
    @PostMapping("media/list")
    public PageVO queryMediaList(PageRequestParams params, @RequestBody QueryMediaModel model) {
        Long companyId = SecurityUtil.getCompanyId();
        return mediaService.queryCourseList(params, model, companyId);
    }

    @GetMapping("/media/preview/{mediaId}")
    public String previewMedia(@PathVariable Long mediaId) {

        Long companyId = SecurityUtil.getCompanyId();

        String vodUrl = mediaService.getVODUrl(mediaId,companyId);

        return vodUrl;
    }
}